// packageA/others/materialCenter/materialCenter.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio: '对私账户',
    is_default: true,
    receiverAccountNoEnc: '', // 银行卡号
    receiverNameEnc: '', // 名称
    receiverBankChannelNo: '', // 账户
    // 判断是否显示对公和对私
    private: false, // 对公
    public: false, // 对私
    type: '', // 类型
    id: '', // 编辑需要的id
    show: false, // 显示删除
    msg: '你确定要删除这个银行卡？', //
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type && options.id) {
      this.setData({
        type: options.type,
        id: options.id
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getShow();
    if (this.data.type == 'edit') {
      this.editInfo();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  // 提交
  addTiJiao() {
    if (app._isTextEmpty(this.data.receiverAccountNoEnc)) {
      wx.showToast({
        title: '请输入银行卡卡号',
        icon: 'none'
      });
      return;
    }
    // var reg = /^\d{16,19}$/;
    var reg = /(^[0-9]*([0-9])?)$/;
    console.log(reg.test(this.receiverAccountNoEnc));

    if (!reg.test(this.data.receiverAccountNoEnc)) {
      wx.showToast({
        // title: '请输入19位的银行卡卡号',
        title: '银行卡卡号只能为纯数字',
        icon: 'none'
      });
      return;
    }
    if (app._isTextEmpty(this.data.receiverNameEnc)) {
      wx.showToast({
        title: '请输入持卡人名称',
        icon: 'none'
      });
      return;
    }
    if (this.data.radio === '对公账户') {
      if (app._isTextEmpty(this.data.receiverBankChannelNo)) {
        wx.showToast({
          title: '请输入对公账户',
          icon: 'none'
        });
        return;
      }
    }

    var type = '';
    if (this.data.radio == '对公账户') {
      type = 204;
    } else if (this.data.radio == '对私账户') {
      type = 201;
    }
    let urlStr = app.getNetAddresss('plugin.converge_pay.frontend.controllers.converge-pay-bank-card.create');
    let json = {
      receiverAccountNoEnc: this.data.receiverAccountNoEnc,
      receiverAccountType: type,
      receiverNameEnc: this.data.receiverNameEnc,
      receiverBankChannelNo: this.data.receiverBankChannelNo,
      default: this.data.is_default ? '1' : '0'
    };
    // json = JSON.stringify(json)
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          });
          this.setData({
            receiverAccountNoEnc: '',
            receiverNameEnc: '',
            receiverBankChannelNo: ''
          });
          setTimeout(() => {
            wx.navigateTo({
              url: '/packageA/others/HJinfo/bankCardInformation/bankCardInformation'
            });
          }, 1000);
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 编辑数据
  editInfo() {
    let urlStr = app.getNetAddresss('plugin.converge_pay.frontend.controllers.converge-pay-bank-card.edit');
    let json = {
      id: this.data.id
    };
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res.data, '96565');

          this.setData({
            receiverAccountNoEnc: res.data.receiverAccountNoEnc,
            receiverAccountType: res.data.receiverAccountType,
            receiverNameEnc: res.data.receiverNameEnc,
            receiverBankChannelNo: res.data.receiverBankChannelNo
          });

          if (app._isTextEmpty(res.data.has_one_default)) {
            this.setData({
              is_default: false
            });
          } else {
            if (res.data.has_one_default.default == '0') {
              this.setData({
                is_default: false
              });
            } else {
              this.setData({
                is_default: true
              });
            }
          }

          if (this.data.receiverAccountType == '204') {
            this.setData({
              radio: '对公账户',
            });
          } else if (this.data.receiverAccountType == '201') {
            this.setData({
              radio: '对私账户',
            });
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 编辑修改
  editTiJiao() {
    if (app._isTextEmpty(this.data.receiverAccountNoEnc)) {
      wx.showToast({
        title: '请输入银行卡卡号',
        icon: 'none'
      });
      return;
    }
    if (app._isTextEmpty(this.data.receiverNameEnc)) {
      wx.showToast({
        title: '请输入持卡人名称',
        icon: 'none'
      });
      return;
    }
    if (this.data.radio === '对公账户') {
      if (app._isTextEmpty(this.data.receiverBankChannelNo)) {
        wx.showToast({
          title: '请输入对公账户',
          icon: 'none'
        });
        return;
      }
    }

    var type = '';
    if (this.data.radio == '对公账户') {
      type = 204;
    } else if (this.data.radio == '对私账户') {
      type = 201;
    }
    let json = {
      receiverAccountNoEnc: this.data.receiverAccountNoEnc,
      receiverAccountType: type,
      receiverNameEnc: this.data.receiverNameEnc,
      receiverBankChannelNo: this.data.receiverBankChannelNo,
      default: this.data.is_default ? '1' : '0',
      id: this.data.id
    };
    let urlStr = app.getNetAddresss('plugin.converge_pay.frontend.controllers.converge-pay-bank-card.edit');
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          });
          setTimeout(() => {
            wx.navigateTo({
              url: '/packageA/others/HJinfo/bankCardInformation/bankCardInformation'
            });
          }, 1000);
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 删除
  deleteChange() {
    wx.showModal({
      title: '提示',
      content: '你确定要删除？',
      success: (res) => {
        if (res.confirm) {
          this.setData({
            show: true
          });
          let urlStr = app.getNetAddresss('plugin.converge_pay.frontend.controllers.converge-pay-bank-card.destroy');
          let json = {
            id: this.data.id
          };
          app._getNetWork({
            url: urlStr,
            data: json,
            success: (resdata) => {
              var res = resdata.data;
              if (res.result == 1) {
                wx.showToast({
                  title: res.msg,
                  icon: 'none'
                });
                wx.navigateTo({
                  url: '/packageA/others/HJinfo/bankCardInformation/bankCardInformation'
                });
              } else {
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 1500
                });
                wx.navigateTo({
                  url: '/packageA/others/HJinfo/bankCardInformation/bankCardInformation'
                });
              }
            },
            fail: function (res) {
              console.log(res);
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  },
  // 判断是否显示
  getShow() {
    let urlStr = app.getNetAddresss('plugin.converge_pay.frontend.controllers.converge-pay-bank-card.create');

    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          if (res.data.all == '1') {
            this.setData({
              private: true,
              public: true,
            });
          } else {
            if (res.data.private == '1') {
              this.setData({
                private: true,
                radio: '对私账户',
              });
            }
            if (res.data.public == '1') {
              this.setData({
                public: true,
                radio: '对公账户',
              });
            }
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  onChange(val) {
    this.setData({
      is_default: val.detail
    });
  },
  onChangeRadio(val) {
    this.setData({
      radio: val.detail
    });
  },
  onChangeOne(val) {
    this.setData({
      receiverAccountNoEnc: val.detail
    });
  },
  onChangeTwo(val) {
    this.setData({
      receiverNameEnc: val.detail
    });
  },
  onChangeThree(val) {
    this.setData({
      receiverBankChannelNo: val.detail
    });
  }

});
